﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Applications.Web.RioWCFWorkflow;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.AppCode.ApplicationNavigation;
using Rio.Framework.Pages.Events;

namespace Rio.Framework.Pages.Workflow.Actions
{
    [InheritedExport(typeof(WorkflowButtonAction))]
    public class IndexWorkflowButtonAction : WorkflowButtonAction
    {
        public IndexWorkflowButtonAction()
        {
            Text = Resx.GetLabel(ResxWorkflow.WorkflowAction_Index);
            Image = new Uri("/Rio.Framework.Pages;component/Assets/Images/import.png", UriKind.Relative);
            UiTypeAction = WorkflowTypeAction.Index;
        }

        protected override void ActionClicked()
        {
            IndexPanelClosed += IndexWorkflowButtonAction_IndexPanelClosed;

            ActivateIndexPanel();
        }

        protected void IndexWorkflowButtonAction_IndexPanelClosed(object sender, IndexedFilesEventArgs e)
        {
            //Uncomment: 8.
            var document = e.IndexDocuments.First();
            if (document != null && e.FolderType != null && e.Organization!=null)
            {
                NavigationHelper.Instance.FolderLinkBuilded += Instance_FolderLinkBuilded;
                NavigationHelper.Instance.GetFolderLink(e.Organization.Identifier, e.FolderType.Identifier, e.IndexDocuments.First().FolderId, e.IndexDocuments.First().FolderIdentifier);
            }
            else
            {
                ExecuteAction();
            }
        }

        private void Instance_FolderLinkBuilded(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            //Execute wkf action and send the folder link in comment
            ExecuteAction(new List<Parameter>()
                            {
                                new Parameter()
                                    {
                                        ParameterName = "COMMENT",
                                        ParameterValue = e.Uri.OriginalString
                                    }
                            });
        }
    }

    public class IndexAllWorkflowButtonAction : IndexWorkflowButtonAction
    {
        public IndexAllWorkflowButtonAction()
            :base()
        {
            Text = Resx.GetLabel(ResxWorkflow.WorkflowAction_IndexAll);
            IsGlobalAction = true;
        }
    }
}